<?php

class WeixinController extends Uuch_BaseController {

    public function loginAction() {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $email = $request->getParam('email');
            $password = $request->getParam('password');
            $fromusername = $request->getParam('uid');
            $user = Application_Model_User::getByEmail($email);
            if ($user != null) {
                $password = $user->generatePassword($password, $user->Passwordsalt);
                if ($password == $user->Password) {
                    //更新用户和微信关系表信息
                    $model = Application_Model_Weixin::getWeixin($fromusername);
                    if ($model == null) {
                        $model = new Application_Model_Weixin();
                        $model->fromusername = $fromusername;
                        $model->uuchid = $user->id;
                        $model->fakeid=0;
                        $model->status = 1;
                        $model->save();
                    }
                    $result['value'] = 1;
                    echo json_encode($result);
                    $this->_helper->layout->disableLayout();
                    exit;
                }
            } else {
                $result['value'] = 0;
                echo json_encode($result);
                $this->_helper->layout->disableLayout();
                exit;
            }
        }
        $this->_helper->layout->disableLayout();
    }

    public function waybillAction() {
        $fromusername = $this->getSafeParam("fromusername");
        $userid = Application_Model_Weixin::getWeixin($fromusername)->uuchid;
        $this->view->waybilllist = Application_Model_Weixin::getTopWayBill($userid, 10);
        $this->view->fromusername = $fromusername;
        $this->_helper->layout->disableLayout();
    }

    public function storeitemAction() {
        $fromusername = $this->getSafeParam("fromusername");
        $userid = Application_Model_Weixin::getWeixin($fromusername)->uuchid;
        $this->view->storeitemlist = Application_Model_Weixin::getTopStoreItem($userid, 10);
        $this->view->fromusername = $fromusername;
        $this->_helper->layout->disableLayout();
    }

    public function waybilldetailAction() {
        $fromusername = $this->getSafeParam("fromusername");
        $billNo = $this->getSafeParam("billNo");
        $model = Application_Model_wayBillItem::bybillNo($billNo);
        $this->view->waybilldetail = $model;
        $this->view->fromusername = $fromusername;
        $this->_helper->layout->disableLayout();
    }

    public function storeitemdetailAction() {
        $fromusername = $this->getSafeParam("fromusername");
        $id = $this->getSafeParam("id");
        $model = Application_Model_Weixin::getStoreitemDetail($id);
        $this->view->storeitemdetail = $model;
        $this->view->fromusername = $fromusername;
        $this->_helper->layout->disableLayout();
    }

    public function indexAction() {
        $model = new Application_Model_Weixin();
        define("TOKEN", "uuch");
        $model->responseWeixin();
    }
    
    

    public function sendAction() {
        $result = Application_Model_Weixin::getAll();
        foreach ($result as $value) {
            $value->delete();
        }
    }

    public function createmenuAction() {
        $menu_ary = array(
            "button" => array(
                //左边第一个菜单
                array(
                    "name" => '我的运单',
                    "type" => "click",
                    "key" => "wayBill"
                ),
                //中间的菜单
                array(
                    "type" => "click",
                    "name" => "我的仓库",
                    "key" => "storeItem"
                ),
                //右边的菜单
                array(
                    "name" => '自助服务',
                    "sub_button" => array(
                        array(
                            "type" => "click",
                            "name" => "查询运费",
                            "key" => "expressFree"
                        ),
                        array(
                            "type" => "click",
                            "name" => "上门服务",
                            "key" => "vistoringService"
                        ),
                    ),
                ),
            )
        );
        $Weixin = new Application_Model_Weixin();
        var_dump($Weixin->creatMenu($menu_ary));
    }

}

?>